import { _decorator, Asset, assetManager, Component, instantiate, Node, Prefab, randomRange, randomRangeInt, Sprite, SpriteFrame } from 'cc';
import { DySprite } from './manager/DySprite';
import { DySpine } from './manager/DySpine';
import { DyAssetsManager } from './manager/DyAssetsManager';
const { ccclass, property } = _decorator;

const spArray = [
    "hero_22_1/hero_22_1",
    "hero_24_1/hero_24_1",
    "hero_41_1/hero_41_1",
    "monster_01_1/monster_01_1",
    "monster_02_1/monster_02_1",
]

/**
 * 预制体引用类型：
 * 1. 直接引用：直接在场景中拖拽预制体，然后在脚本中通过预制体变量引用
 * 2. 动态加载：通过资源管理器加载预制体，然后在脚本中通过预制体变量引用
 * 3. 动态创建：通过instantiate函数动态创建预制体，然后在脚本中通过预制体变量引用
 */

@ccclass('AssetsTest')
export class AssetsTest extends Component {
    @property(DySprite)
    sprite: DySprite = null;

    @property(DySpine)
    spine: DySpine = null;

    index = 0;

    listSp : Node[] = [];

    switch : boolean = false;
    start() {
        let role24 = this.node.getChildByName("sprite_24");
        let nn = DyAssetsManager.instantiateNode(role24)
        nn.setParent(this.node)
        console.log("nn => ",nn);
        
        let nn1 = DyAssetsManager.instantiateNode(nn)
        nn1.setParent(this.node)
        console.log("nn1 => ",nn1);

        this.scheduleOnce(() => {
            DyAssetsManager.Destroy(nn1)
        }, 1)
        this.scheduleOnce(() => {
            DyAssetsManager.Destroy(nn)
        }, 2)
        this.scheduleOnce(() => {
            DyAssetsManager.Destroy(role24)
        }, 3)
    }

    tempTimer : number = 0;
    update(deltaTime: number) {
        this.tempTimer += deltaTime;
        if (this.switch) {
            this.index += 1;
            if (this.index % 2 == 0) {
                let sp = instantiate(this.sprite.node)
                sp.setParent(this.node)
                let rx = randomRange(-300, 300)
                let ry = randomRange(-500, 500)
                sp.setPosition(rx,ry,0)
                this.listSp.push(sp);
                let dysp = sp.getComponent(DySprite)
                let id = randomRangeInt(1, 11);
                dysp.setSprite("items/icon_"+id,"");
            }
            if (this.index % 3 == 0) {
                let spine = instantiate(this.spine.node)
                spine.setParent(this.node)
                let x = randomRange(-300, 300)
                let y = randomRange(-500, 500)
                spine.setPosition(x,y,0)
                this.listSp.push(spine);
                let dyspine = spine.getComponent(DySpine)
                let tid = randomRangeInt(0, 5);
                dyspine.setSpine(spArray[tid],"","idle");
            }
            if (this.index % 30 == 0) {
                for (let i = this.listSp.length-1; i >= 0; i--) {
                    const sp = this.listSp[i];
                    sp.destroy();
                }
                this.listSp = [];
            }
        }
        if (this.tempTimer > 1) {
            this.tempTimer = 0;
            console.log(" assets count => ",assetManager.assets.count);
        }
    }

    onSwitch() {
        this.switch = !this.switch;
    }

    onBtnClick() {
        console.log(" assets => ");
        let list : Asset[] = []
        assetManager.assets.forEach((item, index) => {
            if (item.refCount == 0) {
                console.log("资源：", item.name, item.refCount);
                console.log("item：", item);
                list.push(item);
            }
        });
        for (let i = 0; i < list.length; i++) {
            const element = list[i];
            console.log("释放资源：", element.name, element.refCount);
            assetManager.releaseAsset(element)
        }
        console.log(" assets => ",assetManager.assets);
    }


    // 计算朝向
    direct(){
        // this.node.position = this.p0.position;
        // let dirP = new Vec3();
        // Vec3.subtract(dirP, this.p1.position, this.p0.position);
        // let dirX = new Vec3(1, 0, 0);
        // let angle = Vec3.signedAngle(dirX, dirP, new Vec3(0, 0, 1))
        // let deg = angle * 180 / Math.PI;
        // this.node.angle = deg;
        // console.log("angle=",angle,"  deg = ", deg);
    }
}


